package com.guodi.map.utils;

import com.guodi.map.vo.ShapeModelVO;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.nio.charset.Charset;
import java.util.*;

/**
 * @author 程靓坤
 * @version 1.0
 * @create 2019/7/26 0026 14:35
 * description:读取Shp文件
 */
public class ShapeReaderUtil {

    public ArrayList<ShapeModelVO> readShapeFile(String filePath) {
        ArrayList<ShapeModelVO> modelList = new ArrayList<>();
        File folder = new File(filePath);
        if (!folder.isDirectory()) {
            if (folder.toString().endsWith(".shp")) {
                try {
                    modelList = getShapeFile(folder);
                    return modelList;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("选择的文件后缀名不是.shp");
            }
        }else{
            File[] files = folder.listFiles();
            if (!(files.length > 0)) {
                System.out.println("目录文件为空");
                return modelList;
            }

            for (File file : files) {
                if (!file.toString().endsWith(".shp")) {
                    continue;
                }
                try {
                    ArrayList<ShapeModelVO> models = getShapeFile(file);
                    modelList.addAll(models);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return modelList;
    }

    private ArrayList<ShapeModelVO> getShapeFile(File file) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", file.toURI().toURL());
        ArrayList<ShapeModelVO> models = new ArrayList<>();
        DataStore dataStore = DataStoreFinder.getDataStore(map);
        //字符转码，防止中文乱码
        ((ShapefileDataStore) dataStore).setCharset(Charset.forName("utf8"));
        String typeName = dataStore.getTypeNames()[0];
        FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);
        FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures();
        FeatureIterator<SimpleFeature> features = collection.features();
        while (features.hasNext()) {
            SimpleFeature feature = features.next();
            ShapeModelVO model = new ShapeModelVO();
            Iterator<? extends Property> iterator = feature.getValue().iterator();
            while (iterator.hasNext()) {
                Property property = iterator.next();
                //property数据与实体类对应
                if (property.getName().toString().equals("the_geom")){
                    model.setGeoStr(property.getValue().toString());
                }
                if (property.getName().toString().equals("layer")){
                    model.setLayer(property.getValue().toString());
                }
                if (property.getName().toString().equals("projection")){
                    model.setProjection(property.getValue().toString());
                }
                if (property.getName().toString().equals("region")){
                    model.setRegion(property.getValue().toString());
                }
                if (property.getName().toString().equals("regionCode")){
                    model.setRegionCode(property.getValue().toString());
                }
                if (property.getName().toString().equals("level")){
                    model.setLevel(Integer.parseInt(property.getValue().toString()));
                }
                if (property.getName().toString().equals("time")){
                    model.setTime(property.getValue().toString());
                }
                model.setCreateTime(new Date());
                model.setClazz("");

            }
            models.add(model);
        }
        return models;
    }

    /*public  String getCoordinateSystemWKT(String shpFilePath) {
        ShapefileDataStore dataStore = buildDataStore(shpFilePath);
        try {
            return dataStore.getSchema().getCoordinateReferenceSystem().toWKT();
        } catch (UnsupportedOperationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            dataStore.dispose();
        }
        return "";
    }*/

    /**
     * 构建ShapeDataStore对象。
     * @param shpFilePath shape文件路径。
     * @return
     */
   /* public  ShapefileDataStore buildDataStore(String shpFilePath) {
        ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
        try {
            ShapefileDataStore dataStore = (ShapefileDataStore) factory
                    .createDataStore(new File(shpFilePath).toURI().toURL());
            if (dataStore != null) {
                dataStore.setCharset(Charset.forName("UTF-8"));
            }
            return dataStore;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }*/
}
